<?php
namespace app\index\model;
use think\Db;
use think\Model;

class SubUserInfo extends Model
{
	public function getQuesIds($param){
		$user = user();
		return self::when($param['quesIds'], function($query) use ($param){
						$query->where('ques_id', 'in', $param['quesIds']);
					})
					->where('user_id',$user['id'])
					->order('last_time desc')->column('ques_id');
	}

	public function disposeWrong($type,$quesId,$action = 'insert'){
		$user = user();
		$data = self::where('user_id',$user['id'])->where('type',$type)->where('ques_id',$quesId)->find();
		if($data) self::where('user_id',$user['id'])->where('type',$type)->where('ques_id',$quesId)->update(['last_time'=>time()]);
		if($action == 'insert' && !$data){
			self::insert([
				'type' => $type,
				'user_id' => $user['id'],
				'ques_id' => $quesId,
				'start_time' => time(),
				'last_time' => time(),
			]);
		}else if($action == 'delete'){
 			self::where('user_id',$user['id'])->where('type',$type)->where('ques_id',$quesId)->delete();
		}
	}

	public function question()
	{
		return $this->hasOne('SubQuestion','id','ques_id');
	}

	public function getStartTimeAttr($value)
	{
		return date('Y-m-d H:i:s',$value);
	}
}